﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.JQuery.VSDoc.js" assembly="Dimok.Web.UI"/>
/// <reference name="Dimok.Web.UI.JS.JQuery.Extentions.js" assembly="Dimok.Web.UI"/>
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI"/>
/// <reference path="../../Manager/WebServices/Uploads.asmx" />
/// <reference path="../../JS/UI/Dimok.PageEvents.js" />
/// <reference path="../../JS/UI/Dimok.Table.js" />
Type.registerNamespace("motobankcorp");
motobankcorp.Uploads = function(element) {
  motobankcorp.Uploads.initializeBase(this, [element]);
  this._purchaseBatchID = 0;
  Dimok.Accessorize(this);
  this._onRowContextMenuItemClickedHandler = Function.createDelegate(this, this._onRowContextMenuItemClicked);
  this.set_carPriceMode(Dimok.CarPriceModes.prototype.Upload);
}
motobankcorp.Uploads.prototype = {
  _importUploadMethod: function(uploadID, purchaseBatchID, onSuccess, onError) {
    return Dimok.FindWSMethod("ImportUpload", WSA)(uploadID, purchaseBatchID, onSuccess, onError);
  },
  _decisionUploadMethod: function(programID, uploadID, onSuccess, onError) {
    return Dimok.FindWSMethod("DecisionUpload", WSA)(programID, uploadID, onSuccess, onError);
  },
  initialize: function() {
    motobankcorp.Uploads.callBaseMethod(this, 'initialize');
    this.get_RGE().add_rowContextMenuItemClicked(this._onRowContextMenuItemClickedHandler);

    var pe = this.get_PE();

    //    pe.add_uploadExportError(Function.createDelegate(this, this._batchItemCompleted));
    pe.add_loanDeleted(Function.createDelegate(this, function(sender, eventArgs) {
      if (!eventArgs._inBatch) this.get_RGE().rebind();
    }));
    pe.add_loanBatchDeleted(Function.createDelegate(this, function() {
      this.get_RGE().rebind();
    }));
    pe.add_uploadInfoSelected(Function.createDelegate(this, function(sender, eventArgs) {
      this.get_RGE().set_filterTemp("UploadInfoID", eventArgs.get_keys().get_ID());
    }));
    pe.add_uploadInfoDeselected(Function.createDelegate(this, function() {
      this.get_RGE().set_filterTemp();
    }));
    pe.add_purchaseBatchesSelected(Function.createDelegate(this, function(sender, eventArgs) {
      this.set_purchaseBatchID(eventArgs.get_keys().get_ID());
    }));
    pe.add_purchaseBatchesDeselected(Function.createDelegate(this, function() {
      this.set_purchaseBatchID(0);
    }));
    //    $J(this.get_bookProviders()).change(Function.createCallback(function(e, context) {
    //      var bookValues = Dimok.Ajax({ methodName: "GetCarBookPriceTypes", wsObject: WSA }, [$J(this).val()]);
    //      $J(context.get_bookValueTypes()).fillSelect(bookValues);
    //    }, this));
  },
  dispose: function() {
    this.get_RGE().remove_rowContextMenuItemClicked(this._onRowContextMenuItemClickedHandler);
    motobankcorp.Uploads.callBaseMethod(this, 'dispose');
  },
  _callCarPriceWebMethod: function(keys, bookValueTypeID, onCompleteDelegate) {
    return Dimok.FindWSMethod(this.get_carPriceMode(), WSA)(keys.get_ID(), bookValueTypeID, onCompleteDelegate, onCompleteDelegate);
  },
  _uploadItemByKeys: function(perchaseBatchID, isBatch) {
    if (isBatch === undefined) { this._showLoadPanel(); isBatch = this.get_batchKeys().length > 1; }
    var keys = Dimok.$A(this.get_batchKeys().shift());
    var gridDataItem = this.findGridDataItem(keys) || undefined;
    if (!keys) return this._hideLoadPanel();
    var selfDelegate = Function.createDelegate(this, function() { this._uploadItemByKeys(perchaseBatchID, isBatch); });
    var batchTitle = isBatch ? "Import " : "";
    var onCompleteDelegate = Function.createDelegate(this, function(result) {
      if (!(result || {}).get_message)
        this.get_PE().raise_loanInserted(new Dimok.Event.Loan(null, keys, this.get_batchKeys().length));
      if (true === this._onComplete(result, keys, batchTitle, gridDataItem, selfDelegate))
        this.get_PE().raise_loanBatchInserted();
    });
    this._importUploadMethod(keys.get_ID(), perchaseBatchID, onCompleteDelegate, onCompleteDelegate);
  },
  _decisionByKeys: function(programID, isBatch) {
    if (isBatch === undefined) { this._showLoadPanel(); isBatch = this.get_batchKeys().length > 1; }
    var keys = Dimok.$A(this.get_batchKeys().shift());
    var gridDataItem = this.findGridDataItem(keys) || undefined;
    if (!keys) return this._hideLoadPanel();
    var selfDelegate = Function.createDelegate(this, function() { this._decisionByKeys(programID, isBatch); });
    var batchTitle = isBatch ? "Decision " : "";
    var onCompleteDelegate = Function.createDelegate(this, function(result) {
      this._onComplete(result, keys, batchTitle, gridDataItem, selfDelegate);
    });
    this._decisionUploadMethod(programID, keys.get_ID(), onCompleteDelegate, onCompleteDelegate);
  },
  _onRowContextMenuItemClicked: function(sender, eventArgs) {
    switch (eventArgs._item.get_value()) {
      case "Decision":
        var programID = parseInt(eventArgs.get_item().get_attributes().getAttribute("ProgramID"));
        if (!this.set_batchKeys(true).length || !confirm("Decision " + _loans(this.get_batchKeys()) + "?")) return this.set_batchKeys();
        eventArgs.set_cancel(true);
        return this._decisionByKeys(programID);
      case "Import":
        if (!this.get_purchaseBatchID()) return alert("Select Purchse Batch first.");
        if (!this.set_batchKeys(true).length || !confirm("Import " + _loans(this.get_batchKeys()) + "?")) return this.set_batchKeys();
        eventArgs.set_cancel(true);
        return this._uploadItemByKeys(this.get_purchaseBatchID());
    }
    function _loans(batchKeys) {
      return batchKeys.length == 1 ? Sys.Serialization.JavaScriptSerializer.serialize(batchKeys[0]) + " loan" : batchKeys.length + " loans";
    }
  }
}
motobankcorp.Uploads.registerClass('motobankcorp.Uploads', Dimok.Table);
